package com.app.micaihu.view.army.adapter

import android.view.View
import android.widget.ImageView
import com.app.micaihu.R
import com.app.micaihu.view.bean.ArmyMember
import com.app.micaihu.view.utils.loadCircleImage
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * 官职设置 适配器
 */
class ArmyOfficialPositionSetListAdapter(showSelect: Boolean) : BaseQuickAdapter<ArmyMember.Member, BaseViewHolder>(R.layout.army_official_position_set_list_item) {

    var showSelect = false

    init {
        this.showSelect = showSelect
    }

    override fun convert(holder: BaseViewHolder, item: ArmyMember.Member) {
        holder.apply {
            getView<ImageView>(R.id.ivHeadPic).loadCircleImage(item.avatar, R.drawable.user_head_def)
            setText(R.id.tvNickName, item.nickname)
            if (item.grade.isEmpty()) {
                setGone(R.id.tvGrade, true)
            } else {
                setVisible(R.id.tvGrade, true)
                setText(R.id.tvGrade, item.grade)
            }
            val ivSelect = getView<ImageView>(R.id.ivSelect)
            if (showSelect) {
                if (item.position == "1") {
                    ivSelect.visibility = View.INVISIBLE
                } else {
                    ivSelect.visibility = View.VISIBLE
                }
            } else {
                ivSelect.visibility = View.GONE
            }
            if (item.select) {
                setImageResource(R.id.ivSelect, R.mipmap.army_member_select_on)
            } else {
                setImageResource(R.id.ivSelect, R.mipmap.army_member_select_off)
            }
        }
    }

    fun getSelectSize(): Int {
        var size = 0
        for (item in data) {
            if (item.select) size++
        }
        return size
    }

    /**
     * 返回需要删除的uid , 分割
     */
    fun getSelectUid(): String {
        var uidStr = ""
        for ((index, item) in data.withIndex()) {
            if (item.select) {
                uidStr = if (index == 0) {
                    uidStr.plus(item.uid)
                } else {
                    uidStr.plus(",").plus(item.uid)
                }
            }
        }
        return uidStr
    }
}